package ystp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ystp.model.Shengchanqiyelinxuan;
import ystp.model.Shengchanqiyelinxuanzhongjianjieguo;
import ystp.model.Ysrenyuanfenpei;
import ystp.model.Ysxiangmu;
import ystp.service.*;
import ystp.support.JsonResult;
import ystp.support.PageInfo;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("shengchanqiyezhongjianjieguo")
public class ShengchanqiyelinxuanzhongjianjieguoController {

    public Logger log = LoggerFactory.getLogger(ShengchanqiyelinxuanzhongjianjieguoController.class);

    @Autowired
    public ShengchanqiyelinxuanzhongjianjieguoService ddService;
    @Autowired
    public YsrenyuanfenpeiService ddYsrenyuanfenpeiService;
    @Autowired
    public ShengchanqiyelinxuanService ddShengchanqiyelinxuanService;
    @Autowired
    public ShengchanqiyelinxuantoupiaoService ddShengchanqiyelinxuantoupiaoService;
    @Autowired
    public YsxiangmuService ddYsxiangmuService;

    @RequestMapping("getZhongjianjieguo")
    public JsonResult<?> getResultCount(@RequestParam("xiangmuid") String ppXiangmuId){
        try {
            Ysxiangmu mmYsxiangmu=ddYsxiangmuService.findOne(ppXiangmuId);
            String mmXiangmuleixing = mmYsxiangmu.getXiangmuleixing();//项目类型ID 11 12 13 14
            int mmLunci = mmYsxiangmu.getLunci();

            List<Ysrenyuanfenpei> mmUserList = ddYsrenyuanfenpeiService.findUserByXiangmuId(ppXiangmuId);
            Double guize1 = ((double)2/(double)3)*mmUserList.size();//表1 1-48品种
            Double guize2 = ((double)1/(double)2)*mmUserList.size();//其余

            int mmTongguopiaoshu1 = (int)Math.ceil(guize1);//表1 1-48品种
            int mmTongguopiaoshu2 = (int)Math.ceil(guize2);//其余

            List<Shengchanqiyelinxuan> mmList = ddShengchanqiyelinxuanService.findAllByXiangmuId(ppXiangmuId);
            for (int i=0;i<mmList.size();i++){
                String mmDetailId = mmList.get(i).getId();
                int mmPinzhong = Integer.parseInt(mmList.get(i).getPinguixuhao());
                int Score = ddShengchanqiyelinxuantoupiaoService.getOneScore(mmDetailId,mmLunci);

                Shengchanqiyelinxuan mmShengchanqiyelinxuan = ddShengchanqiyelinxuanService.findOne(mmDetailId);
                mmShengchanqiyelinxuan.setBeizhu12(String.valueOf(Score));
                ddShengchanqiyelinxuanService.modify(mmShengchanqiyelinxuan);

                int mmJieguoText = 0;
                if (mmXiangmuleixing.equals("11")&&mmPinzhong>0&&mmPinzhong<49){
                    if (Score>mmTongguopiaoshu1){
                        mmJieguoText = 1;
                    }else{
                        mmJieguoText = 0;
                    }
                }else{
                    if (Score>mmTongguopiaoshu2){
                        mmJieguoText = 1;
                    }else{
                        mmJieguoText = 0;
                    }
                }

                Shengchanqiyelinxuanzhongjianjieguo mmShengchanqiyelinxuanzhongjianjieguo = ddService.findByDetailId(mmDetailId);
                if (mmShengchanqiyelinxuanzhongjianjieguo!=null){
                    mmShengchanqiyelinxuanzhongjianjieguo.setLunci(mmLunci);
                    mmShengchanqiyelinxuanzhongjianjieguo.setScore(Score);
                    mmShengchanqiyelinxuanzhongjianjieguo.setStatus(mmJieguoText);
                    ddService.modify(mmShengchanqiyelinxuanzhongjianjieguo);
                }else{
                    mmShengchanqiyelinxuanzhongjianjieguo = new Shengchanqiyelinxuanzhongjianjieguo();
                    mmShengchanqiyelinxuanzhongjianjieguo.setId(UUID.randomUUID().toString());
                    mmShengchanqiyelinxuanzhongjianjieguo.setDetailid(mmDetailId);
                    mmShengchanqiyelinxuanzhongjianjieguo.setLunci(mmLunci);
                    mmShengchanqiyelinxuanzhongjianjieguo.setScore(Score);
                    mmShengchanqiyelinxuanzhongjianjieguo.setStatus(mmJieguoText);
                    mmShengchanqiyelinxuanzhongjianjieguo.setCreatetime(new Date());
                    mmShengchanqiyelinxuanzhongjianjieguo.setPinzhong(mmList.get(i).getPinguixuhao());
                    mmShengchanqiyelinxuanzhongjianjieguo.setPingui(mmList.get(i).getPinguixuhao());
                    ddService.add(mmShengchanqiyelinxuanzhongjianjieguo);
                }

            }

            return JsonResult.getSuccessResult("中间结果生成成功");
        }catch (Exception e) {
            e.printStackTrace();
            log.error("shengchanqiyezhongjianjieguo/getZhongjianjieguo:error",e);
            return JsonResult.getErrorResult("shengchanqiyezhongjianjieguo/getZhongjianjieguo:error " + e.getMessage());
        }
    }

    @RequestMapping("getHuizongbiao")
    public JsonResult<?> getHuizongbiao(@RequestParam("xiangmuid") String ppxiangmuId, @RequestParam("pageindex") int ppPageIndex,
                                               @RequestParam("pagesize") int ppPageSize){
        try {
            Ysxiangmu mmYsxiangmu=ddYsxiangmuService.findOne(ppxiangmuId);
            int mmLunci = mmYsxiangmu.getLunci();

            String mmLunciText = "";

            List<Map<String,Object>> mmShengchanList = new ArrayList<>();
            List<Map<String,Object>> mmPinzhongAll = ddShengchanqiyelinxuanService.findPinzhongCustom(ppxiangmuId,ppPageIndex,ppPageSize);
            System.out.println(mmPinzhongAll);
            for (int i = 0; i < mmPinzhongAll.size(); i++) {
                int mmPinzhongHebingshu = 0;
                Map<String,Object> mmMap = mmPinzhongAll.get(i);
                if (mmMap!=null){
                    List<Map<String,Object>> mmPinguiList = new ArrayList<>();
                    String mmPinzhongOne = mmMap.get("pinzhongbianhao") == null ? "" : mmMap.get("pinzhongbianhao").toString();
                    List<Map<String,Object>> mmPinguilist = ddShengchanqiyelinxuanService.findPinguiByPinzhong(ppxiangmuId,mmPinzhongOne);
                    if (mmPinguilist.size()>0){
                        mmPinzhongHebingshu = mmPinguilist.size();
                    }else{
                        mmPinzhongHebingshu = mmPinzhongHebingshu+1;
                    }
                    List<Map<String,Object>> mmPinguiFenlei = ddShengchanqiyelinxuanService.findPinguiFenlei(ppxiangmuId,mmPinzhongOne);
                    for (int j = 0; j < mmPinguiFenlei.size(); j++) {
                        int mmPinguiHebingshu = 0;
                        Map<String,Object> mmPinguiMap = mmPinguiFenlei.get(j);
                        if (mmPinguiMap!=null){
                            String mmPinguiOne = mmPinguiMap.get("pinguixuhao") == null ? "" : mmPinguiMap.get("pinguixuhao").toString();
                            List<Map<String,Object>> mmShengchanqiyelist = ddShengchanqiyelinxuanService.findShengchanqiyeByPinguiScore(ppxiangmuId,mmPinguiOne);
                            if (mmShengchanqiyelist.size()>0){
                                mmPinguiHebingshu = mmShengchanqiyelist.size();
                            }else{
                                mmPinguiHebingshu = mmPinguiHebingshu+1;
                            }
                            mmPinguiMap.put("shengchanqiyelist",mmShengchanqiyelist);
                            mmPinguiMap.put("pinguihebinghang",mmPinguiHebingshu);
                            mmPinguiList.add(mmPinguiMap);
                        }
                    }
                    mmMap.put("pinguilist",mmPinguiList);
                    mmMap.put("pinzhonghebinghang",mmPinzhongHebingshu);
                }
                mmShengchanList.add(mmMap);
            }
            Integer mmCount = ddShengchanqiyelinxuanService.getPinguiCount(ppxiangmuId,String.valueOf(mmLunci),ppPageIndex,ppPageSize);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("ShengchanList", mmShengchanList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("shengchanqiyezhongjianjieguo/getHuizongbiao:error",e);
            return JsonResult.getErrorResult("shengchanqiyezhongjianjieguo/getHuizongbiao:error " + e.getMessage());
        }
    }

    @RequestMapping("checknextlunci")
    public JsonResult<?> checknextlunci(@RequestParam("xiangmuid") String ppxiangmuId){
        try {
            Ysxiangmu mmYsxiangmu=ddYsxiangmuService.findOne(ppxiangmuId);
            int mmLunci = mmYsxiangmu.getLunci();

            List<Ysrenyuanfenpei> mmUserList = ddYsrenyuanfenpeiService.findUserByXiangmuId(ppxiangmuId);

            List<Map<String, Object>> mmList = ddShengchanqiyelinxuanService.findPinguiAll(ppxiangmuId,1,9999);

            boolean next = false;

            int num = 0;

            for (int i=0;i<mmList.size();i++){
                String mmPingui = mmList.get(i).get("pinguixuhao").toString();

                //224,533,787,789排除
                if (!mmPingui.equals("224")&&!mmPingui.equals("533")&&!mmPingui.equals("787")&&!mmPingui.equals("789")){
                    int mmCountPass = ddService.countPassCustom(mmPingui,mmLunci,ppxiangmuId);
                    System.out.println(mmPingui+":"+mmCountPass);
                    if (mmCountPass<1){
                        num++;
                        System.out.println(num);
                        next = true;
                        List<Map<String, Object>> mmList2 = ddShengchanqiyelinxuanService.findShengchanqiyeByPingui(ppxiangmuId,mmPingui);
                        for (int j=0;j<mmList2.size();j++){
                            String mmDetailId = mmList2.get(j).get("id").toString();
                            Shengchanqiyelinxuan mmOne = ddShengchanqiyelinxuanService.findOne(mmDetailId);
                            mmOne.setSuijilunci(String.valueOf(mmLunci+1));
                            ddShengchanqiyelinxuanService.modify(mmOne);

                            ddService.deleteByPingui(mmPingui,mmDetailId);
                        }
                    }
                }
            }

            if (next){
                mmYsxiangmu.setLunci(mmLunci+1);
                ddYsxiangmuService.modify(mmYsxiangmu);

                for (int i=0;i<mmUserList.size();i++){
                    String mmId = mmUserList.get(i).getId();
                    Ysrenyuanfenpei mmRy = ddYsrenyuanfenpeiService.findOne(mmId);
                    mmRy.setIssubmit(0);
                    ddYsrenyuanfenpeiService.update(mmRy);
                }
            }

            if (next){
                System.out.println("aa");
                return JsonResult.getSuccessResult("共有"+num+"个品规未选出生产企业，需进行下一轮投票");
            }else{
                return JsonResult.getErrorResult("无需进入下一轮");
            }

        }catch (Exception e) {
            e.printStackTrace();
            log.error("shengchanqiyezhongjianjieguo/checknextlunci:error",e);
            return JsonResult.getErrorResult("shengchanqiyezhongjianjieguo/checknextlunci:error " + e.getMessage());
        }
    }
}
